<!--
 * @Description: 
 * @version: 
 * @Author: 尹鹏孝
 * @Date: 2022-01-06 11:09:50
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2022-01-06 11:28:26
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <style>
        .drag-container {
            display: flex;
            justify-content: flex-start;
            align-items: center;
        }

        .drag-container .drag-box {
            display: flex;
            align-items: center;
            justify-content: center;

            margin: 5px;
            color: #fff;
            font-size: 20px;
            font-weight: bold;
        }

        .drag-container .drag-box .drag-item {
            display: flex;
            align-items: center;
            justify-content: center;

        }
    </style>
    <title>ng-dragger</title>
    <script src="https://cdn.bootcss.com/angular.js/1.3.1/angular.js"></script>
    <script src="https://cdn.bootcss.com/ngDraggable/0.1.11/ngDraggable.js"></script>
</head>

<body ng-app="app" ng-controller="draggerController">
    <div class="drag-container">
        <div ng-repeat="item in tenants" class="drag-box" ng-drop="true"
            ng-drop-success="onDropComplete($index, $data, $event)">
            <div class="drag-item" ng-drag="true" ng-drag-data="{{item}}" style="background:#000;color:#fff">
                {{item.text}}</div>
        </div>
    </div>

    <script>
        var myApp = angular.module('app', ['ngDraggable']);
        myApp.controller('draggerController', function ($scope) {
            $scope.tenants = [{
                text: '方块1',
                color: 'green',
                key: 'arr1'
            }, {
                text: '方块2',
                color: 'blue',
                key: 'arr1'
            }, {
                text: '方块3',
                color: '#888',
                key: 'arr1'
            }, {
                text: '方块4',
                color: '#888',
                key: 'arr2'
            }, {
                text: '方块5',
                color: '#888',
                key: 'arr2'
            }, {
                text: '方块6',
                color: '#888',
                key: 'arr2'
            }];
            $scope.onDropComplete = function (index, obj, $event) {
                var otherObj = $scope.tenants[index];
                var otherIndex = $scope.tenants.findIndex(vv => Object.is(vv.text, obj.text));
                if (otherObj.key !== obj.key) return;
                $scope.tenants[index] = obj;
                $scope.tenants[otherIndex] = otherObj;
            }
        })
    </script>
</body>

</html>